ImagXpress 13 for .NET - User Guide > How To > Reduce a JPEG Image |
Use the JpegReduce method to reduce JPEGs in memory or on disk without first decompressing to a DIB, thereby saving processor time and system resources.
Only 8-bpp grayscale and 24-bpp color JPEGs are supported. |
File size reduction is accomplished by requantizing the source JPEG.
Compression ratios and perceived image quality will vary depending on the source JPEG. Generally, higher JPEG compression comes at the cost of visual quality. For this reason, an optional qualityFactor argument is provided to balance compression versus visual quality trade-offs.
Larger qualityFactor values correspond to larger luminance and chrominance values, which may degrade visual quality. For example, consider the 2592x3872, 24-bit color sequential JPEG in Figure 1 that is reduced using various qualityFactor values:
An optional transformMode argument is provided to save reduced JPEGs as either Sequential or Progressive JPEG.
A Progressive JPEG consists of multiple image scans in progressively higher detail. JPEG viewers that support Progressive JPEGs can display the lower quality scans while the remainder of the file is read into memory, which may improve the user experience for viewing large images.
Saving to Progressive JPEG may result in slightly larger files when compared to JPEGs reduced and saved as Sequential JPEG. For example, consider again the 2592x3872, 24-bit color sequential JPEG in Figure 1 that is reduced using various qualityFactor values and converted to Progressive JPEG:
Figure 1: An example 24-bit color sequential JPEG. Dimensions 2592 x 3872. Original size is 2746 KB.
Figure 2: An example 24-bit color sequential JPEG, reduced from 2746 KB to 420 KB with qualityFactor 10.
Figure 3: An example 24-bit color sequential JPEG, reduced from 2746 KB to 246 KB with qualityFactor 22.
Figure 4: An example 24-bit color sequential JPEG, reduced from 2746 KB to 196 KB with qualityFactor 32.
To reduce JPEGs on disk, just specify the path to the source JPEG and the path to save the re-compressed JPEG:
C# Example |
Copy Code
|
---|---|
try { using (Accusoft.ImagXpressSdk.ImagXpress ix = new Accusoft.ImagXpressSdk.ImagXpress()) { Accusoft.ImagXpressSdk.ImageReduce.JpegReduce( ix, "Arches_Portrait.jpg", "Arches_Portrait.reduced.jpg"); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } |
Optionally specify the qualityFactor to affect compression and image quality and transformMode to affect the JPEG format saved:
C# Example |
Copy Code
|
---|---|
try { using (Accusoft.ImagXpressSdk.ImagXpress ix = new Accusoft.ImagXpressSdk.ImagXpress()) { Int32 qualityFactor = 10; Accusoft.ImagXpressSdk.ImageReduce.JpegReduce( ix, "Arches_Portrait.jpg", "Arches_Portrait.reduced.jpg", qualityFactor, TransformMode.ToProgressiveJpeg); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } |
The JpegReduce method can use JPEGs expressed in memory. First read the source JPEG data into Byte array and then re-compress the JPEG data:
C# Example |
Copy Code
|
---|---|
try { // Read JPEG file to a Byte array. String jpegPath = "Arches_Portrait.jpg"; FileInfo fileInfo = new FileInfo(jpegPath); Byte[] original = new Byte[fileInfo.Length]; using (FileStream fileStream = new FileStream( jpegPath, FileMode.Open, FileAccess.Read)) { fileStream.Read(original, 0, (int)fileInfo.Length); } // Reduce JPEG to a Byte array. Int32 qualityFactor = 10; Byte[] reduced; using (Accusoft.ImagXpressSdk.ImagXpress ix = new Accusoft.ImagXpressSdk.ImagXpress()) { Accusoft.ImagXpressSdk.ImageReduce.JpegReduce( ix, original, out reduced, qualityFactor, TransformMode.ToProgressiveJpeg); Console.Write("QualityFactor {0} reduced \"{1}\" from {2} bytes to {3} bytes.", qualityFactor, jpegPath, original.Length, reduced.Length ); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } |